package yt.kpool.activity.setting.agent.myguest;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.adapter.GuestChildAountListviewAdapter;
import yt.kpool.adapter.ModeSelectListviewAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.GuestChildAountBean;
import yt.kpool.bean.KeyLoginBean;
import yt.kpool.bean.RecommendCustomersBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 客户子账户
 */
public class GuestChildAountActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    @BindView(R.id.lv_select)
    ListView lv_select;
    private GuestChildAountListviewAdapter guestChildAountListviewAdapter;
    private List<GuestChildAountBean.DataBean> list;

    public static void start(Activity mactivity, String UserId, String CurrencyId, String Currencycode) {
        Intent intent = new Intent(mactivity, GuestChildAountActivity.class);
        intent.putExtra("UserId", UserId);
        intent.putExtra("CurrencyId", CurrencyId);
        intent.putExtra("Currencycode", Currencycode);
        mactivity.startActivity(intent);
    }


    public String getUserId() {
        return getIntent().getStringExtra("UserId");
    }

    public String getCurrencyId() {
        return getIntent().getStringExtra("CurrencyId");
    }

    public String getCurrencycode() {
        return getIntent().getStringExtra("Currencycode");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guest_child_aount);
        ButterKnife.bind(this);
        initTitle();
        initlist();
        getdata();
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("子账户列表");

        loadingDialog = new LoadingDialog(this, 0);
    }

    private void initlist() {
        list = new ArrayList<>();
        guestChildAountListviewAdapter = new GuestChildAountListviewAdapter(this, list);
        guestChildAountListviewAdapter.setBlanceunit(getCurrencycode());
        lv_select.setAdapter(guestChildAountListviewAdapter);

        lv_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (list.size() > i) {
                    fastLogin(list.get(i).getChildAccountId()+"");
                }


            }
        });
    }


    /**
     * 快速登录
     *
     * @param ChildAccountID
     */
    private void fastLogin(String ChildAccountID) {
        if (NetWorkUtil.check(this)) {


            loadingDialog.show();
            NetHelperNew.ChildAccountIDLogin(ChildAccountID, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "快速查看返回结果：" + data);
                    KeyLoginBean keyLoginBean = MyGson.getInstance().fromJson(data, KeyLoginBean.class);
                    if (keyLoginBean.getType() == 1) {

                        SPUtil spUtil = new SPUtil(BaseApplication.context, BaseApplication.KEYLOGIN);

                        spUtil.putString(BaseApplication.NAME, keyLoginBean.getData().getLoginModel().getUserName());
                        spUtil.putString(BaseApplication.ID, keyLoginBean.getData().getLoginModel().getUserID() + "");
                        spUtil.putString(BaseApplication.ChildAccountId, keyLoginBean.getData().getChildAccount().getChildAccountId() + "");
                        spUtil.putString(BaseApplication.ChildAccountName, keyLoginBean.getData().getChildAccount().getChildAccountName());
                        Intent intent = new Intent(GuestChildAountActivity.this, MainActivity.class);
                        intent.putExtra("From","GC");
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);

                    } else {
                        ToastUtil.showToast(getApplicationContext(), keyLoginBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    /**
     * 获取子账户列表
     */
    private void getdata() {

        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.ChildAccountForUserID(getCurrencyId(), getUserId(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "客户子账户列表：" + data);
                    GuestChildAountBean guestChildAountBean = MyGson.getInstance().fromJson(data, GuestChildAountBean.class);
                    if (guestChildAountBean.getType() == 1) {
                        if (guestChildAountBean.getData().size() > 0) {

                            list.addAll(guestChildAountBean.getData());
                            guestChildAountListviewAdapter.notifyDataSetChanged();

                        } else {
                            ToastUtil.showToast(GuestChildAountActivity.this, "没有可查询数据");
                        }
                    } else {

                        new ErrorDialog(GuestChildAountActivity.this, guestChildAountBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(GuestChildAountActivity.this, msg);
                }
            });


        }

    }

}
